#ifndef CMATRIX_H
#define CMATRIX_H

struct CrossWordItem
{
	std::wstring word;
	std::wstring file;
	int accent;
	int pos;
};

class CMatrix
{
public:
	struct Item
	{
		wchar_t s;
		bool accent;
		Item(wchar_t s_ = L' ', bool accent_ = false):s(s_), accent(accent_) {};
	};

	CMatrix();
	~CMatrix();

	void init(int rows);
	bool setAnchor(const CrossWordItem& item);
	bool addWord(const CrossWordItem& item);
	const CrossWordItem& getHorizWord(int row) const;
	const CrossWordItem& getVertWord(int row) const;
	const Item& get(size_t row, size_t col) const;

	void dump();

private:
	std::vector<std::vector<Item> > m_items;
	std::vector<CrossWordItem> m_horizWords;
	std::vector<CrossWordItem> m_vertWords;
	bool m_mustIntersect;
	enum { VERT, HORIZ } m_direction;

	std::pair<int, int> findRow(int stepY, int stepX, const CrossWordItem& item) const;
	void putAtRow(int row, int stepX, const CrossWordItem& item);
	std::pair<int, int> findCol(int stepY, int stepX, const CrossWordItem& item) const;
	void putAtCol(int col, int stepY, const CrossWordItem& item);
	bool canPlace(int row, int col, wchar_t s) const;
	bool canPlace(int row, int stepX, const std::wstring& s) const;
	inline wchar_t at(int row, int col) const;
};

bool readWords(const char* file, std::vector<CrossWordItem>& items);

#endif
